package com.ruoyi.wzgl.cgjh.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.wzgl.cgjh.domain.Cgjh;
import com.ruoyi.wzgl.cgjh.domain.CgjhMx;
import com.ruoyi.wzgl.cgjh.mapper.CgjhMapper;
import com.ruoyi.wzgl.cgjh.service.ICgjhService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.*;

/**
 * 采购计划Service业务层处理
 *
 * @author huacai
 * @date 2025-04-07
 */
@Service
public class CgjhServiceImpl implements ICgjhService {
    @Autowired
    private CgjhMapper cgjhMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询采购计划
     *
     * @param cgjhId 采购计划主键
     * @return 采购计划
     */
    @Override
    public Cgjh selectCgjhByCgjhId(String cgjhId) {
        return cgjhMapper.selectCgjhByCgjhId(cgjhId);
    }

    /**
     * 查询采购计划列表
     *
     * @param cgjh 采购计划
     * @return 采购计划
     */
    @Override
    public List<Cgjh> selectCgjhList(Cgjh cgjh) {
        return cgjhMapper.selectCgjhList(cgjh);
    }

    /**
     * 新增采购计划
     *
     * @param cgjh 采购计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCgjh(Cgjh cgjh) {
        cgjh.setCgjhId(IdUtils.fastSimpleUUID());
        cgjh.setCreateBy(getUsername());
        cgjh.setUserId(Math.toIntExact(getUserId()));
        cgjh.setDeptId(Math.toIntExact(getDeptId()));
        cgjh.setCreateTime(DateUtils.getNowDate());
        int rows = cgjhMapper.insertCgjh(cgjh);
        insertCgjhMx(cgjh);
        return rows;
    }

    /**
     * 批量新增采购计划
     *
     * @param cgjhs 采购计划List
     * @return 结果
     */
    @Override
    public int batchInsertCgjh(List<Cgjh> cgjhs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(cgjhs)) {
            try {
                for (int i = 0; i < cgjhs.size(); i++) {
                    int row = cgjhMapper.insertCgjh(cgjhs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == cgjhs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改采购计划
     *
     * @param cgjh 采购计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCgjh(Cgjh cgjh) {
        cgjh.setUpdateTime(DateUtils.getNowDate());
        cgjh.setUpdateBy(getUsername());
        cgjhMapper.deleteCgjhMxByCgjhId(cgjh.getCgjhId());
        insertCgjhMx(cgjh);
        return cgjhMapper.updateCgjh(cgjh);
    }

    /**
     * 批量删除采购计划
     *
     * @param cgjhIds 需要删除的采购计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCgjhByCgjhIds(String[] cgjhIds) {
        cgjhMapper.deleteCgjhMxByCgjhIds(cgjhIds);
        return cgjhMapper.deleteCgjhByCgjhIds(cgjhIds);
    }

    /**
     * 删除采购计划信息
     *
     * @param cgjhId 采购计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCgjhByCgjhId(String cgjhId) {
        cgjhMapper.deleteCgjhMxByCgjhId(cgjhId);
        return cgjhMapper.deleteCgjhByCgjhId(cgjhId);
    }

    /**
     * 新增计划单明细信息
     *
     * @param cgjh 采购计划对象
     */
    public void insertCgjhMx(Cgjh cgjh) {
        List<CgjhMx> cgjhMxList = cgjh.getCgjhMxList();
        String cgjhId = cgjh.getCgjhId();
        if (StringUtils.isNotNull(cgjhMxList)) {
            List<CgjhMx> list = new ArrayList<CgjhMx>();
            for (CgjhMx cgjhMx : cgjhMxList) {
                cgjhMx.setCgjhId(cgjhId);
                list.add(cgjhMx);
            }
            if (list.size() > 0) {
                cgjhMapper.batchCgjhMx(list);
            }
        }
    }
}
